//
//  RevampPassViewController.m
//  Village
//
//  Created by longrise on 2017/9/21.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "RevampPassViewController.h"
#import "RevampTableViewCell.h"
@interface RevampPassViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic , strong) UITableView *mytableview;
@property (nonatomic ,strong) NSMutableArray *leftary;
@property (nonatomic ,strong) NSMutableArray *tishiary;
@property (nonatomic ,strong) UIButton *surebutton;
@property (nonatomic ,strong) NSString *stra;
@property (nonatomic ,strong) NSString *strb;
@end

@implementation RevampPassViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kBackgroundColor;

    self.title = @"修改密码";
     _tishiary =[[NSMutableArray alloc] initWithObjects:@"请输入旧密码",@"请输入新密码",nil];
     _leftary =[[NSMutableArray alloc] initWithObjects:@"旧密码",@"新密码",nil];
    _mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight) style:UITableViewStylePlain];
    _mytableview.delegate = self;
    _mytableview.dataSource = self;
    _mytableview.scrollEnabled =NO; //设置tableview 不能滚动
    [self.view addSubview:_mytableview];

    UIView *footview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KDeviceHeight, 600)];
    footview.backgroundColor = kBackgroundColor;
    _mytableview.tableFooterView = footview;
    
    _surebutton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_surebutton setTitle:@"确定" forState:UIControlStateNormal];
    _surebutton.tintColor = [UIColor whiteColor];
    _surebutton.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
    _surebutton.titleLabel.font = [UIFont systemFontOfSize:18*kScreenScaleX];
    _surebutton.frame = CGRectMake(30*kScreenScaleX, 20*kScreenScaleY, 315*kScreenScaleX, 45*kScreenScaleY);
    //    [_surebutton setBackgroundImage:[UIImage imageNamed:@"BG"]forState:UIControlStateNormal];
    _surebutton.backgroundColor = K_Anniuhong;
    [_surebutton addTarget:self action:@selector(sureaction) forControlEvents:UIControlEventTouchUpInside];
    [footview addSubview:_surebutton];

}
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 返回行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}
// 设置cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    static NSString *cellID = @"id";
    RevampTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[RevampTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor whiteColor];
    }
    
    if (indexPath.row == 0 ){
        cell.gainbutton.hidden = YES;
        cell.seebutton.hidden = NO;
        
    }else  if (indexPath.row == 1 ){
        cell.gainbutton.hidden = YES;
        cell.seebutton.hidden = NO;
        
    }
    cell.textfield.secureTextEntry = YES;
    cell.textfield.tag = indexPath.row;
    cell.textfield.delegate = self;
    [cell.textfield addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    cell.leftlable.text = _leftary[indexPath.row];
    cell.textfield.placeholder = _tishiary[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//cell点击之后没有视觉效果
    
    return cell;
}
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 50*kScreenScaleY;
}
- (void)textFieldWithText:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            _stra = textField.text;
            break;
        case 1:
            _strb = textField.text;

            break;
    
       
        default:
            break;
    }
}
- (void)sureaction{
    
//     BOOL Result = [_stra compare:_strb];
    if (!self.stra){
        [MBProgressHUD showError:@"请输入旧密码！" toView:self.navigationController.view];
    }else if (![Tools checkPassWordRationality:self.strb]) {
        [MBProgressHUD showError:@"请输入6~20位的字母和数字组成的新密码！" toView:self.navigationController.view];
//    }else if (Result == 0) {
//        [MBProgressHUD showError:@"旧密码与新密码一样,无需修改！" toView:self.navigationController.view];
    }
    else{
        NSMutableDictionary *bean = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:[Globle getInstance].account.userid forKey:@"id"];
        [dict setObject:_stra forKey:@"passwordcode"];
        [dict setObject:_strb forKey:@"newpassword"];
        [bean setValue:dict forKey:@"entitybean"];
        
        [NetRequestClass Post:UpdatePasswordURL Parameters:bean success:^(id result) {
            [MBProgressHUD hideHUDForView:self.navigationController.view];
            [MBProgressHUD showSuccess:result[@"message"] toView:self.navigationController.view Completion:nil];
            [[Globle getInstance] saveAccountInfo:result[@"data"]];
            [[Globle getInstance] cleanAllObject];
           
            [[NSNotificationCenter  defaultCenter] postNotificationName:RefreshLoginInfo object:nil];
            UIViewController *viewCtl = self.navigationController.viewControllers[0];
            [self.navigationController popToViewController:viewCtl animated:YES];
//            [self tap];
            LSLog(@"msg -----  %@",result);
        } failure:^(id valueCode, id error) {
            [MBProgressHUD hideHUDForView:self.navigationController.view];
            
            if ([error isEqualToString:@"密码错误"]){
                
                [MBProgressHUD showError:@"旧密码输入错误" toView:self.navigationController.view];
            }else{
                 [MBProgressHUD showError:error toView:self.navigationController.view];
            }
        }];
    }
    
   

    
    
    
}

- (void)tap{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    id obj = [LongriseRouter openURL:@"LoginController"];
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:dict block:^(id obj) {
              
            }];
        }
    }
    if (obj && [obj isKindOfClass:[UIViewController class]]) {
        UIViewController *VC = obj;
        VC.hidesBottomBarWhenPushed = YES;
       
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
        [self presentViewController:nav animated:YES completion:nil];
//        [self.navigationController pushViewController:VC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
